<script lang="ts" setup>
import useSocketStore from '../../store/socketStore'
import {  setToken } from '../../utils/auth'
import { onMounted, onUnmounted } from 'vue';
import {getSocket} from '../../api/index'
const socketStore = useSocketStore();

onMounted(()=>{

    getSocket().then(()=>{})
    // let ws = new WebSocket('ws://1.94.185.57:8082/tokenWebSocket/1')
    let ws = new WebSocket('ws://192.168.1.41:8082/tokenWebSocket/1')
    // 连接打开时触发  
    ws.onopen = function() {  
        console.log('Connection opened');  
        // 可以在这里发送消息  
        ws.send('Hello Server!');  
    };  
      
    // 接收到服务器消息时触发  
    ws.onmessage = function(event) {  
        console.log('Received from server: ', event.data);  
        setToken(event.data)
    };  
      
    // 连接关闭时触发  
    ws.onclose = function() {  
        console.log('Connection closed');  
    };  
      
    // 发生错误时触发  
    ws.onerror = function(error) {  
        console.log('WebSocket Error: ', error);  
    };  
    // let token= getToken();
  
    // socketStore.connection(url, token);

  })
  
  
   
   
onUnmounted(()=>{
  socketStore.closeWs();
})
</script>